{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.编写一个程序，输出1到100之间所有的素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "5\n",
      "7\n",
      "11\n",
      "13\n",
      "17\n",
      "19\n",
      "23\n",
      "29\n",
      "31\n",
      "37\n",
      "41\n",
      "43\n",
      "47\n",
      "53\n",
      "59\n",
      "61\n",
      "67\n",
      "71\n",
      "73\n",
      "79\n",
      "83\n",
      "89\n",
      "97\n"
     ]
    }
   ],
   "source": [
    "def is_prime(num):\n",
    "    '''判断一个数字num是否为素数'''\n",
    "    if num <= 1:\n",
    "        return False\n",
    "    for i in range(2,num):\n",
    "        if num % i == 0:\n",
    "            return False\n",
    "    else:\n",
    "        return True\n",
    "\n",
    "# 输出1到100之间所有的素数   \n",
    "for num in range(1,101):\n",
    "    if is_prime(num):\n",
    "        print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.编写一个函数，接收一个字符串作为参数，返回该字符串中每个单词的首字母大写的版本。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"I'm A College Student.\""
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def convert_str(s):\n",
    "    words = s.split()\n",
    "    return \" \".join([word.capitalize() for word in words])\n",
    "\n",
    "s = \"I'm a college student.\"\n",
    "convert_str(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.编写一个程序，读取一个文本文件的内容，并将其中的每个单词（以空格分隔）及其出现的次数统计出来，最后输出每个单词及其出现次数."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The:1\n",
      "Python:2\n",
      "interpreter:1\n",
      "is:2\n",
      "easily:1\n",
      "extended:1\n",
      "with:1\n",
      "new:1\n",
      "functions:1\n",
      "and:1\n",
      "data:1\n",
      "types:1\n",
      "implemented:1\n",
      "in:1\n",
      "C:1\n",
      "or:1\n",
      "C++:1\n",
      "(or:1\n",
      "other:1\n",
      "languages:1\n",
      "callable:1\n",
      "from:1\n",
      "C).:1\n",
      "also:1\n",
      "suitable:1\n",
      "as:1\n",
      "an:1\n",
      "extension:1\n",
      "language:1\n",
      "for:1\n",
      "customizable:1\n",
      "applications.:1\n"
     ]
    }
   ],
   "source": [
    "# 读取文件并切分\n",
    "with open('test.txt') as f:\n",
    "    words = f.read().split()\n",
    "wordsdict = {}\n",
    "for word in words:\n",
    "    wordsdict[word] = wordsdict.get(word,0)+1\n",
    "for word,count in wordsdict.items():\n",
    "    print(word+\":\"+str(count))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.编写一个简单的学生成绩管理系统，能够添加学生成绩、计算平均分、查找最高分和最低分等功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "学生成绩管理系统\n",
      "1. 添加成绩\n",
      "2. 计算平均分\n",
      "3. 查找最高分\n",
      "4. 查找最低分\n",
      "5. 显示所有成绩\n",
      "6. 退出\n"
     ]
    }
   ],
   "source": [
    "class StudentScoreManager:\n",
    "    def __init__(self):\n",
    "        self.scores = []\n",
    "\n",
    "    def add_score(self, score):\n",
    "        if not isinstance(score, (int, float)) or score < 0 or score > 100:\n",
    "            print(\"请输入一个有效的成绩（0到100之间的整数或浮点数）\")\n",
    "            return\n",
    "        self.scores.append(score)\n",
    "        print(f\"成绩 {score} 已添加。\")\n",
    "\n",
    "    def calculate_average(self):\n",
    "        if not self.scores:\n",
    "            print(\"没有成绩记录，无法计算平均分。\")\n",
    "            return\n",
    "        average = sum(self.scores) / len(self.scores)\n",
    "        print(f\"平均分是: {average:.2f}\")\n",
    "\n",
    "    def find_highest(self):\n",
    "        if not self.scores:\n",
    "            print(\"没有成绩记录，无法查找最高分。\")\n",
    "            return\n",
    "        highest = max(self.scores)\n",
    "        print(f\"最高分是: {highest}\")\n",
    "\n",
    "    def find_lowest(self):\n",
    "        if not self.scores:\n",
    "            print(\"没有成绩记录，无法查找最低分。\")\n",
    "            return\n",
    "        lowest = min(self.scores)\n",
    "        print(f\"最低分是: {lowest}\")\n",
    "\n",
    "    def display_scores(self):\n",
    "        if not self.scores:\n",
    "            print(\"没有成绩记录。\")\n",
    "            return\n",
    "        print(\"所有成绩记录:\")\n",
    "        for score in self.scores:\n",
    "            print(score)\n",
    "\n",
    "\n",
    "def main():\n",
    "    manager = StudentScoreManager()\n",
    "    \n",
    "    while True:\n",
    "        print(\"\\n学生成绩管理系统\")\n",
    "        print(\"1. 添加成绩\")\n",
    "        print(\"2. 计算平均分\")\n",
    "        print(\"3. 查找最高分\")\n",
    "        print(\"4. 查找最低分\")\n",
    "        print(\"5. 显示所有成绩\")\n",
    "        print(\"6. 退出\")\n",
    "\n",
    "        choice = input(\"请选择操作: \")\n",
    "\n",
    "        if choice == '1':\n",
    "            score = float(input(\"请输入成绩: \"))\n",
    "            manager.add_score(score)\n",
    "        elif choice == '2':\n",
    "            manager.calculate_average()\n",
    "        elif choice == '3':\n",
    "            manager.find_highest()\n",
    "        elif choice == '4':\n",
    "            manager.find_lowest()\n",
    "        elif choice == '5':\n",
    "            manager.display_scores()\n",
    "        elif choice == '6':\n",
    "            print(\"退出系统。\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效的选择，请重新选择。\")\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.编写冒泡排序算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 5, 7]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def bubble_sort(lst):\n",
    "    n = len(lst)\n",
    "    for i in range(n):\n",
    "        for j in range(n-i-1):\n",
    "            if lst[j]>lst[j+1]:\n",
    "                lst[j],lst[j+1] = lst[j+1],lst[j]\n",
    "    return lst\n",
    "\n",
    "bubble_sort([5,7,2,1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "da",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
